<?php
namespace app\admin\controller;
use app\common\util\UploadUtil;
use app\common\util\TreeUtil;
use \think\Db;

/**
 * 基类
 * @author xiegaolei
 *
 */
class Base extends \think\Controller
{	
    public function _initialize(){
    	 //未登录
    	if (is_login()==0) {
    		$this->redirect('login/index');
    	}
    	 
		$myrules = Db::name('auth_group')->where('id',session('admin_auth.roleId'))->value('rules');
    	$myrules = explode(',', $myrules);
    	$mymenus = Db::name('auth_rule')->where(['id'=>['in',$myrules]])->order('sorts',' asc')->select();
   
    	$mymenus = TreeUtil::listToTreeMulti( $mymenus ,  0 , 'id' , 'pid' , 'child');
    	
    	$module = strtolower(request()->module()); 
    	$controller = strtolower(request()->controller());
    	$action = strtolower(request()->action());
    	$nowUrl = $module.'/'.$controller.'/'.$action;
    	 
		$this->assign('mymenus',$mymenus);
		$this->assign('module',$module);
		$this->assign('controller',$controller);
		$this->assign('action',$action);
		$this->assign('nowUrl',$nowUrl);
    }
    
    /**
     * 上传图片
     */
    public function upimage(){
    	
    	$path = UploadUtil::upimage('img');
    	ajax_return_ok(['url'=>$path],'上传成功！');
    }
    
    
    /**
     * 删除上传图片
     */
    public function delimage(){
    	
    	$path = trim($_POST['path']);
    	if (empty($path)) {
    		ajax_return_error('路径不存在');
    	}
    	//相对index.php 修正路径
    	$path = str_replace('/tp5/public', '.', $path);
    	
    	if(file_exists($path)){
    		$exts = config('system.thumb_image_ext');
    		foreach ($exts as $v){
    			unlink($path.$v['width'].'_'.$v['height'].$v['ext']);
    		}
    		unlink($path);
    		ajax_return_ok([],'删除成功！');
    	}else{
    		ajax_return_error('图片不存在');
    	}
    }
    
    
    
  
    
    
}
